package com.ask.ui;

/**
 *
 * @author suryakencana
 */
import com.explodingpixels.macwidgets.HudWidgetFactory;
import com.explodingpixels.macwidgets.HudWindow;
import java.awt.*;
import javax.swing.*;

public class JNotifWindow extends HudWindow {

    // Width of the toster
    private int toasterWidth = 300;
    // Height of the toster
    private int toasterHeight = 80;
    // Step for the toaster
    private int step = 20;
    // Step time
    private int stepTime = 30;
    // Show time
    private int displayTime = 4000;
    // Current number of toaster...
    private int currentNumberOfToaster = 0;
    // Last opened toaster
    private int maxToaster = 0;
    // Max number of toasters for the sceen
    private int maxToasterInSceen;
    // Background image
    private Image backgroundImage;
    // Font used to display message
    private Font font;
    // Color for border
    private Color borderColor;
    // Color for toaster
    private Color toasterColor;
    // Set message color
    private Color messageColor;
    // Set the margin
    int margin;
    // Flag that indicate if use alwaysOnTop or not.
    // method always on top start only SINCE JDK 5 !
    boolean useAlwaysOnTop = true;
    private JDialog hudDialog;
    private JLabel iconLabel = HudWidgetFactory.createHudLabel("");
    private JTextArea message = new JTextArea();
    private int setMultiWindows = 0;
    public static int setKali = 0;

    public JNotifWindow() {
        iniComponent();
    }

    private void iniComponent() {

        message.setMargin(new Insets(5, 5, 5, 5));
        message.setBackground(Color.BLACK);
        message.setForeground(Color.WHITE);
        message.setEditable(false);

        message.setSize(240, 130);
        message.setLineWrap(true);
        message.setWrapStyleWord(true);
        //message.setText("shdjasfgdjasdgasdasd\n ajshdkashgdjasvdaj\n gasfdjhas");

        JPanel pp = new JPanel(new BorderLayout(1, 2));
        pp.setBackground(Color.DARK_GRAY);
        pp.add(iconLabel, BorderLayout.WEST);
        pp.add(message, BorderLayout.CENTER);
        getContentPane().add(pp);
    }

    public void animate() {
        //(new JNotifWindow.Animation(hudDialog)).start();
        new Animation(hudDialog).start();
    }

    /**
     * Show a toaster with the specified message and the associated icon.
     */
    public static void showNotif(Icon icon, String msg, Boolean anim) {

        JNotifWindow jnotif = new JNotifWindow();
        jnotif.hudDialog = jnotif.getJDialog();
        jnotif.hudDialog.setAlwaysOnTop(true);
        jnotif.hudDialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jnotif.message.setText(msg);

        if (icon != null) {
            jnotif.iconLabel.setIcon(icon);
        }
        if (anim) {
            jnotif.hudDialog.setSize(350, 100);
            jnotif.setMultiWindows = jnotif.hudDialog.getHeight() * JNotifWindow.setKali;
            jnotif.hideCloseButton();
            jnotif.hudDialog.setVisible(true);
            jnotif.animate();

        } else {
            jnotif.hudDialog.setSize(300, 350);
            jnotif.hudDialog.setLocationRelativeTo(null);
            jnotif.hudDialog.setVisible(true);
        }
    }

    /**
     * Show a toaster with the specified message.
     */
    public static void showNotif(String msg) {
        showNotif(null, msg, false);
    }

    public static void showNotif(Icon icon, String msg) {
        showNotif(icon, msg, false);
    }

    public static void showNotif(String msg, Boolean anim) {
        showNotif(null, msg, anim);
    }

    class Animation extends Thread {

        //Toaster.SingleToaster toaster;
        JDialog toaster;

        public Animation(JDialog toaster) { //Toaster.SingleToaster toaster) {
            this.toaster = toaster;
        }

        /**
         * Animate vertically the toaster. The toaster could be moved from
         * bottom to upper or to upper to bottom
         *
         * @param posx
         * @param fromy
         * @param toy
         * @throws InterruptedException
         */
        protected void animateVertically(int posx, int fromY, int toY) throws InterruptedException {

            toaster.setLocation(posx, fromY);
            if (toY < fromY) {
                for (int i = fromY; i > toY; i -= step) {
                    toaster.setLocation(posx, i);
                    Thread.sleep(stepTime);
                }
            } else {
                for (int i = fromY; i < toY; i += step) {
                    toaster.setLocation(posx, i);
                    Thread.sleep(stepTime);
                }
            }
            toaster.setLocation(posx, toY);
        }

        @Override
        public void run() {
            try {
                boolean animateFromBottom = true;
                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                Rectangle screenRect = ge.getMaximumWindowBounds();

                int screenHeight = (int) screenRect.height;

                int startYPosition;
                int stopYPosition;

                if (screenRect.y > 0) {
                    animateFromBottom = false; // Animate from top!
                }

                maxToasterInSceen = screenHeight / toasterHeight;

                int posx = (int) screenRect.width - toasterWidth - 1;

                toaster.setLocation(posx, screenHeight);
                toaster.setVisible(true);
                if (useAlwaysOnTop) {
                    toaster.setAlwaysOnTop(true);
                }

                if (animateFromBottom) {
                    startYPosition = screenHeight;
                    stopYPosition = startYPosition - toasterHeight - 1;
                    if (currentNumberOfToaster > 0) {
                        stopYPosition = stopYPosition - (maxToaster % maxToasterInSceen * toasterHeight);
                    } else {
                        maxToaster = 0;
                    }
                } else {
                    startYPosition = screenRect.y - toasterHeight;
                    stopYPosition = screenRect.y;

                    if (currentNumberOfToaster > 0) {
                        stopYPosition = stopYPosition + (maxToaster % maxToasterInSceen * toasterHeight);
                    } else {
                        maxToaster = 0;
                    }
                }

                currentNumberOfToaster++;
                maxToaster++;

                if (setKali > 0) {
                    stopYPosition += setMultiWindows;
                }
                animateVertically(posx, startYPosition, stopYPosition);
                Thread.sleep(displayTime);
                animateVertically(posx, stopYPosition, startYPosition);

                currentNumberOfToaster--;
                toaster.setVisible(false);
                toaster.dispose();
            } catch (Exception e) {
            }
        }
    }
}
